const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function date_time(val) {
  var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
  //月份为0-11，所以+1，月份小于10时补个0
  var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  var hour = date.getHours();
  var minute = date.getMinutes();
  var second = date.getSeconds();
  var theTime = date.getFullYear() + "-" + month + "-" + currentDate + " " + hour + ":" + minute + ":" + second;
  return theTime;
}

function js_date_time(unixtime) {
  var dateTime = new Date(parseInt(unixtime))
  var year = dateTime.getFullYear();
  var month = dateTime.getMonth() + 1 < 10 ? "0" + (dateTime.getMonth() + 1) : dateTime.getMonth() + 1;
  var day = dateTime.getDate() < 10 ? "0" + dateTime.getDate() : dateTime.getDate();
  var hour = dateTime.getHours() < 10 ? "0" + dateTime.getHours() : dateTime.getHours();
  var minute = dateTime.getMinutes() < 10 ? "0" + dateTime.getMinutes() : dateTime.getMinutes();
  var second = dateTime.getSeconds() < 10 ? "0" + dateTime.getSeconds() : dateTime.getSeconds();
  var now = new Date();
  var now_new = Date.parse(now.toDateString()); //typescript转换写法
  var milliseconds = now_new - dateTime;
  var timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
  return timeSpanStr;
}

function js_date_time2(unixtime) {
  var dateTime = new Date(parseInt(unixtime))
  var year = dateTime.getFullYear();
  var month = dateTime.getMonth() + 1 < 10 ? "0" + (dateTime.getMonth() + 1) : dateTime.getMonth() + 1;
  var day = dateTime.getDate() < 10 ? "0" + dateTime.getDate() : dateTime.getDate();
  var hour = dateTime.getHours() < 10 ? "0" + dateTime.getHours() : dateTime.getHours();
  var minute = dateTime.getMinutes() < 10 ? "0" + dateTime.getMinutes() : dateTime.getMinutes();
  var second = dateTime.getSeconds() < 10 ? "0" + dateTime.getSeconds() : dateTime.getSeconds();
  var now = new Date();
  var now_new = Date.parse(now.toDateString()); //typescript转换写法
  var milliseconds = now_new - dateTime;
  var timeSpanStr = year + '年' + month + '月';
  return timeSpanStr;
}

function js_date_time3(unixtime) {
  var dateTime = new Date(parseInt(unixtime))
  var year = dateTime.getFullYear();
  var month = dateTime.getMonth() + 1 < 10 ? "0" + (dateTime.getMonth() + 1) : dateTime.getMonth() + 1;
  var day = dateTime.getDate() < 10 ? "0" + dateTime.getDate() : dateTime.getDate();
  var hour = dateTime.getHours() < 10 ? "0" + dateTime.getHours() : dateTime.getHours();
  var minute = dateTime.getMinutes() < 10 ? "0" + dateTime.getMinutes() : dateTime.getMinutes();
  var second = dateTime.getSeconds() < 10 ? "0" + dateTime.getSeconds() : dateTime.getSeconds();
  var now = new Date();
  var now_new = Date.parse(now.toDateString()); //typescript转换写法
  var milliseconds = now_new - dateTime;
  var timeSpanStr = month;
  return timeSpanStr;
}

function js_date_time4(unixtime) {
  var dateTime = new Date(parseInt(unixtime))
  var year = dateTime.getFullYear();
  var month = dateTime.getMonth() + 1 < 10 ? "0" + (dateTime.getMonth() + 1) : dateTime.getMonth() + 1;
  var day = dateTime.getDate() < 10 ? "0" + dateTime.getDate() : dateTime.getDate();
  var hour = dateTime.getHours() < 10 ? "0" + dateTime.getHours() : dateTime.getHours();
  var minute = dateTime.getMinutes() < 10 ? "0" + dateTime.getMinutes() : dateTime.getMinutes();
  var second = dateTime.getSeconds() < 10 ? "0" + dateTime.getSeconds() : dateTime.getSeconds();
  var now = new Date();
  var now_new = Date.parse(now.toDateString()); //typescript转换写法
  var milliseconds = now_new - dateTime;
  var timeSpanStr = day;
  return timeSpanStr;
}

function js_date_times(unixtime) {
  var dateTime = new Date(parseInt(unixtime))
  var hour = dateTime.getHours() < 10 ? "0" + dateTime.getHours() : dateTime.getHours();
  var minute = dateTime.getMinutes() < 10 ? "0" + dateTime.getMinutes() : dateTime.getMinutes();
  var second = dateTime.getSeconds() < 10 ? "0" + dateTime.getSeconds() : dateTime.getSeconds();
  var now = new Date();
  var now_new = Date.parse(now.toDateString()); //typescript转换写法
  var milliseconds = now_new - dateTime;
  var timeSpanStr = hour + ':' + minute;
  return timeSpanStr;
}

function js_date_times2(unixtime) {
  var dateTime = new Date(parseInt(unixtime))
  var hour = dateTime.getHours() < 10 ? "0" + dateTime.getHours() : dateTime.getHours();
  var minute = dateTime.getMinutes() < 10 ? "0" + dateTime.getMinutes() : dateTime.getMinutes();
  var second = dateTime.getSeconds() < 10 ? "0" + dateTime.getSeconds() : dateTime.getSeconds();
  var now = new Date();
  var now_new = Date.parse(now.toDateString()); //typescript转换写法
  var milliseconds = now_new - dateTime;
  var timeSpanStr = minute;
  return timeSpanStr;
}

function formatTimeTwo(number, format) {
  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

  // var apidomain = "https://www.chouchouya.cn/"; //系统用的
// var apidomain = "https://experience.chouchouya.cn/"; //测试用
var _isid = null;

function getIsId() {
  if (_isid) {
    return _isid;
  }
  _isid = wx.getStorageSync('isid');
  return _isid;

}

function request(req) {
  var isid = getIsId();
  wx.request({
    url: apidomain + req.url,
    success: req.success,
    fail: req.fail,
    complete: req.complete,
    method: "POST",
    header: {
      "Cookie": "isid=" + isid,
    },
    data: req.data
  })
}

function imageUtil(e) {
  var imageSize = {};
  var originalWidth = e.detail.width; //图片原始宽
  var originalHeight = e.detail.height; //图片原始高
  var originalScale = originalHeight / originalWidth; //图片高宽比
  //获取屏幕宽高
  wx.getSystemInfo({
    success: function(res) {
      var windowWidth = res.windowWidth;
      var windowHeight = res.windowHeight;
      // console.log('windowHeight: ' + windowHeight)
      var windowscale = windowHeight / windowWidth; //屏幕高宽比
      if (originalScale < windowscale) { //图片高宽比小于屏幕高宽比
        //图片缩放后的宽为屏幕宽
        imageSize.imageWidth = windowWidth;
        imageSize.imageHeight = (windowWidth * originalHeight) / originalWidth;
      } else { //图片高宽比大于屏幕高宽比
        //图片缩放后的高为屏幕高
        imageSize.imageHeight = windowHeight;
        imageSize.imageWidth = (windowHeight * originalWidth) / originalHeight;
      }

    }
  })
  return imageSize;
}

function myFormatTime(timeStamp) { //传入时间戳差,获取距离当前时间值
  var d = Math.floor(timeStamp / 1000 / 60 / 60 / 24);
  var hour = Math.floor(timeStamp / 1000 / 60 / 60 % 24);
  var min = Math.floor(timeStamp / 1000 / 60 % 60);
  var sec = Math.floor(timeStamp / 1000 % 60);
  if (d < 1) {
    if (hour < 1) {
      if (min < 1) {
        return '刚刚'
      } else if (min >= 1) {
        return min + '分钟前'
      }
    } else if (hour >= 1 && hour < 24) {
      return hour + '小时前'
    }
  } else if (d >= 1 && d < 2) {
    return '昨天'
  } else {
    return d + '天前'
  }
  return countDownTime
}


module.exports = {
  formatTime: formatTime,
  formatTimeTwo: formatTimeTwo,
  date_time: date_time,
  js_date_time: js_date_time,
  js_date_times: js_date_times,
  js_date_times2: js_date_times2,
  js_date_time2: js_date_time2,
  js_date_time3: js_date_time3,
  js_date_time4: js_date_time4,
  request: request,
  imageUtil: imageUtil,
  myFormatTime: myFormatTime
}